/**
 * 深度优先遍历，递归
 * @param {TreeNode} root
 * @return {number}
 */

const minDepth = root => {
  if (!root) return 0
  if (!root.left && !root.right) return 1
  let min = Infinity
  if (root.left) {
    min = Math.min(min, minDepth(root.left))
  }
  if (root.right) {
    min = Math.min(min, minDepth(root.right))
  }
  return min + 1
}

const root = {
  val: 1,
  left: {
    val: 2,
    left: null,
    right: {
      val: 5,
      left: null,
      right: null
    }
  },
  right: {
    val: 3,
    left: null,
    right: null
  }
}

console.log(minDepth(root))